package com.zxx.threadModule;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

import com.zxx.common.Common;
import com.zxx.sql.houseData;
import com.zxx.sql.sqlMan;
import com.zxx.sql.sqlParam;


public class handleThreadModule<Token> extends HandlerThread {
	
	private sqlMan man;
	static final String TAGhandler ="handleThreadTest";
	
			
	private Handler mHandler; 
	/**
	 * @brief 发送给主线程数据库处理结果的handle
	 */
	private Handler mResponseHandler; 
	private Listener<Token> mhandlerListener;
	private List<houseData> mhouselist;
	private sqlParam msqlParam; //sql相关参数
	
	public handleThreadModule(Handler responseHandler) {
		super(TAGhandler);
		man = sqlMan.get(null);
		mResponseHandler = responseHandler;
		mhouselist = new ArrayList<houseData>();
		msqlParam = null;
    }
	/**
	 * @brief 自定义监听器，用于返回数据库处理的结果，刷新ui
	 * 
	 * @param <Token>
	 */
	 public interface Listener<Token> {
	        void onUpdateTextView(List<houseData> list,boolean bcleanAll,sqlParam param);
	    }
	 public void setListener(Listener<Token> listener) {
		 mhandlerListener = listener;
	    }
	 
	/**
	 * @brief ui线程发送消息操作数据库
	 * @param whatmsg 消息命令
	 * @param arg1    消息附加参数1，此处附加的是列表position
	 * @param arg2   消息附加参数2，暂时为空
	 * @param obj  目标，用于传输sql相关参数,参见sqlparam
	 */
	public void SendMsg(int whatmsg,int arg1,int arg2,Object obj) {
		 mHandler
         .obtainMessage(whatmsg,arg1,arg2,obj)
         .sendToTarget();
	}

	/**
	 * @brief 消息的处理
	 */
	@SuppressLint("HandlerLeak")
	@Override
	protected void onLooperPrepared() {
		// TODO Auto-generated method stub
		mHandler = new Handler()
		{
			
			//This Handler class should be static or leaks might occur 
			public void handleMessage(Message msg)
			{
				switch(msg.what)
				{
					case  Common.MESSAGE_INSERTSQL:
					{
						if(0 == man.insertData()) {
							
						}
						mhouselist.clear();
						msqlParam = (sqlParam)msg.obj;
						//查找符合条件的列数
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.selectSQLDefault(selectIndex,selectnum));
						//第一次加载
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
					}
					break;  
					case Common.MESSAGE_SELECTSQLAREA: {
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getdepartmentAllRow(msg.arg1);
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.selectSQLByDepart(msg.arg1,selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
					}
					break; 
					case Common.MESSAGE_SELECTTOTALSQLPRICE: {
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getTotalPriceAllrow(msg.arg1);
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.selectSQLByTotalPrice(msg.arg1,selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
							
					}
					break; 
					case Common.MESSAGE_SORTBYPRICEH_L: {
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.sort_byPriceH_L(selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);	
					}
					break;
					case Common.MESSAGE_SORTBYPRICEL_H: {
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.sort_byPriceL_H(selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
					}
					break;
					case Common.MESSAGE_SORTBYYEARH_L:{
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.sort_byYearH_L(selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);	
					}
					break;
					case Common.MESSAGE_SORTBYYEARL_H:{
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.sort_byYearL_H(selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
					}
					break;
					case Common.MESSAGE_SORTBYAREAH_L:{
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.sort_byAreaH_L(selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);	
					}
					break;
					case Common.MESSAGE_SORTBYAREAL_H:{
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.sort_byAreaL_H(selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);		
					}
					break;
					case Common.MESSAGE_SORTBYALL: {
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getSqlAllRow();
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.selectSQLDefault(selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);		
					}
					break;
					case Common.MESSAGE_SELECTBYHOUSETYPE:{
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getHouseTypeRow(msg.arg1);
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.selectSQLByHouseType(msg.arg1,selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
					}
					break;
					case Common.MESSAGE_SELECTBYZX:{
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getZXTypeRow(msg.arg1);
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.selectSQLByZX(msg.arg1,selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
					}
					break;
					case Common.MESSAGE_SELECTBYPMPRICE:{
						mhouselist.clear();
						//查找总列数
						msqlParam = (sqlParam)msg.obj;
						int totalrow = man.getPmPriceAllrow(msg.arg1);
						int selectIndex = msqlParam.getMselectIndex();
						int selectnum = msqlParam.getMselectnum();
						msqlParam.setMtotalRow(totalrow);
						mhouselist.addAll(man.selectSQLByPmPrice(msg.arg1,selectIndex,selectnum));
						setMysqlParam(selectIndex,selectnum,totalrow,msqlParam);
					}
					break;
					default:
						break;
					
				}
				
			}
		};
	}
	/**
	 * @brief 设置sqlParam参数并通知ui刷新
	 * @param selectIndex 已经选择的项目，用于下拉刷新
	 * @param selectnum   选择项目数量，用于数据库的limit m,n
	 * @param totalrow    该项目共有多少
	 * @param sqldata     设置的sqlparam
	 */
	private void setMysqlParam(int selectIndex,int selectnum,int totalrow,sqlParam sqldata){
		//第一次加载
		if(0 == selectIndex){
			if(selectIndex+selectnum >totalrow){
				sqldata.setMselectIndex(totalrow);
			}else
			{
				sqldata.setMselectIndex(selectIndex+selectnum);
			}
			requestUi(true,sqldata);//删除列表，重新加载数据
		}else{
			//第N次加载
			if(selectIndex+selectnum >totalrow){
				sqldata.setMselectIndex(totalrow);
			}else
			{
				sqldata.setMselectIndex(selectIndex+selectnum);
			}
			requestUi(false,sqldata);//已有数据，直接叠加？
		}
	}
	/**
	 * @brief 通知ui刷新
	 * @param brefresh 是否刷新ui的mhouselist列表,true是，false否
	 * @param param sqlParam参数
	 */
	private void requestUi(final boolean brefresh,final sqlParam param) {
			mResponseHandler.post(new Runnable() {
	            public void run() {
	            	if(null != mhouselist) {
	            		mhandlerListener.onUpdateTextView(mhouselist,brefresh,param);
	            		}
	            }
	        });
	}
	/**
	 * @brief 清除handle队列
	 */
	 public void clearQueue() {
		 
	        mHandler.removeMessages(msqlParam.getCmd());
	        mResponseHandler.removeMessages(msqlParam.getCmd());
	        
	    }
}


